package com.step.ems.pages.urgent

import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.step.ems.R
import com.step.ems.api.UrgentStatus
import com.step.ems.api.UrgentType
import com.step.ems.api.res.UrgentDetailRes
import com.step.ems.component.ExpandableSection
import com.step.ems.component.ItemInfo
import com.step.ems.component.LoadingBox
import com.step.ems.component.MyTopBar
import com.step.ems.component.TextArea
import com.step.ems.component.TextAreaDisable
import com.step.ems.ui.theme.ContentBg
import com.step.ems.ui.theme.DarkBlue
import com.step.ems.ui.theme.DividerColor

@Composable
fun UrgentDetailScreen(
    viewModel: UrgentDetailViewModel = hiltViewModel(),
) {
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    val data by viewModel.data.collectAsStateWithLifecycle()
    Scaffold(topBar = {
        MyTopBar(title = stringResource(R.string.urgentDetail))
    }) { padding ->
        LoadingBox(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding),
            state = state
        ) {
            Content(data, viewModel)
        }
    }
}

@Composable
fun Content(data: UrgentDetailRes, viewModel: UrgentDetailViewModel) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(ContentBg)
            .verticalScroll(rememberScrollState())
    ) {
        Spacer(modifier = Modifier.height(1.dp))
        StatusBar(data, viewModel)
        Spacer(modifier = Modifier.height(12.dp))
        ExpandableSection(title = stringResource(R.string.basicInfo), expand = true) {
            Column(
                modifier = Modifier.fillMaxWidth()
            ) {
                if (data.emergency.emergencyStatus == UrgentStatus.Arrow.value) {
                    HorizontalDivider(color = DividerColor)
                    TextArea(stringResource(R.string.faultReason), viewModel.faultReason)
                    HorizontalDivider(color = DividerColor)
                    TextArea(stringResource(R.string.handleMethod), viewModel.handleMethod)
                } else if (data.emergency.emergencyStatus > UrgentStatus.Arrow.value) {
                    HorizontalDivider(color = DividerColor)
                    TextAreaDisable(stringResource(R.string.faultReason), data.emergency.causeFailure)
                    HorizontalDivider(color = DividerColor)
                    TextAreaDisable(stringResource(R.string.handleMethod), data.emergency.treatmentMode)
                }
                UrgentDetailItem(R.string.urgentNo, data.emergency.emergencyNo)
                UrgentDetailItem(R.string.urgentCaller, data.emergency.alarmPerson)
                UrgentDetailItem(R.string.urgentCallTime, data.emergency.callTime)
                UrgentDetailItem(R.string.urgentCallerPhone, data.emergency.alarmPhoneNumber)
                UrgentDetailItem(R.string.urgentType, stringResource(id = UrgentType.getItem(data.emergency.alarmType)?.titleResId ?: R.string.none))
            }
        }
    }
}

@Composable
fun StatusBar(data: UrgentDetailRes, viewModel: UrgentDetailViewModel) {
    Row(
        modifier = Modifier
            .background(Color.White)
            .padding(horizontal = 16.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(stringResource(R.string.orderStatus), fontSize = 15.sp)
        Text(
            stringResource(id = UrgentStatus.getTitleRes(data.emergency.emergencyStatus)),
            fontSize = 18.sp,
            color = DarkBlue,
            modifier = Modifier.weight(1f)
        )
        if (data.emergency.emergencyStatus in setOf(0, 1, 2)) {
            Button(
                modifier = Modifier.height(32.dp),
                shape = RoundedCornerShape(2.dp),
                colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF5AB6E2)),
                contentPadding = PaddingValues(horizontal = 20.dp, vertical = 0.dp),
                onClick = { viewModel.changeStatus() }) {
                Text(text = stringResource(id = getAction(data.emergency.emergencyStatus)), fontSize = 14.sp)
            }
        }
    }
}

@Composable
fun UrgentDetailItem(
    @StringRes titleId: Int,
    value: String
) {
    HorizontalDivider(color = DividerColor)
    ItemInfo(
        titleId,
        value,
        Modifier.padding(horizontal = 18.dp, vertical = 11.dp)
    )
}

fun getAction(status: Int): Int {
    return when (status) {
        0 -> R.string.urgentActionAccept
        1 -> R.string.urgentActionArrow
        2 -> R.string.urgentActionComplete
        else -> R.string.none
    }
}